package top.laoshuzi.designpatterns.base.created.prototype;

import org.junit.Test;

/**
 * 客户端
 */
public class Client {

    @Test
    public void operation() {

        //初始化原型1原型2
        Prototype p1 = new ConcretePrototype1();
        p1.setName("this is ConcretePrototype1");
        Prototype p2 = new ConcretePrototype2();
        p2.setName("this is ConcretePrototype2");

        //注册到原型管理器
        PrototypeManager.registerPrototype("Prototype1", p1);
        PrototypeManager.registerPrototype("Prototype2", p2);

        //获取原型克隆
        try {
            Prototype prototype1 = PrototypeManager.getPrototype("Prototype1");
            Prototype prototype2 = PrototypeManager.getPrototype("Prototype2");

            System.out.println("prototype1:" + prototype1.getName());
            System.out.println("p1:" + p1.getName());
            System.out.println("prototype1:" + System.identityHashCode(prototype1));
            System.out.println("p1:" + System.identityHashCode(p1));
            System.out.println("prototype2:" + prototype2.getName());
            System.out.println("p2:" + p2.getName());
            System.out.println("prototype2:" + System.identityHashCode(prototype2));
            System.out.println("p2:" + System.identityHashCode(p2));

        } catch (Exception e) {
            e.printStackTrace();
        }

        //从原型管理器注销
        PrototypeManager.unregisterPrototype("Prototype1");
        PrototypeManager.unregisterPrototype("Prototype2");
    }

}
